﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Valor.DBUtility.MSSQLHelper;
using xTeam.HengLe.Entity;
using System.Data.SqlClient;

namespace xTeam.HengLe.DataAccess
{
    public class NewsDataAccess
    {
        /// <summary>
        /// 数据库连接字符串
        /// </summary>
        private static string _con;
        public static string Con
        {
            get { return _con; }
            set { _con = value; }
        }

        public NewsDataAccess() { }

        /// <summary>
        /// 新闻的添加修改
        /// </summary>
        /// <param name="news">新闻实体</param>
        /// <returns>新闻编号</returns>
        public static int Edit(ENews news)
        {
            object[] paramvalues = new object[] { 
                news.NewsId, news.Title, news.Contents, news.Author, news.Source, news.Hits, 
                news.Isvalid, news.Keywords, news.Description, news.ClassifyId, news.SortId,news.ImageName };
            StoreProcedure sp = new StoreProcedure(_con, "news_update");
            int ret = Convert.ToInt32(sp.ExecuteScalar(paramvalues));
            return ret;
        }
        /// <summary>
        /// 删除新闻
        /// </summary>
        /// <param name="newsId">新闻编号</param>
        /// <returns>是否成功</returns>
        public static int Delete(int newsId)
        {
            object[] paramvalues = new object[] { newsId };
            StoreProcedure sp = new StoreProcedure(_con, "news_delete");
            int ret = sp.ExecuteNonQuery(paramvalues);
            return ret;
        }
        /// <summary>
        /// 条件查询新闻
        /// </summary>
        /// <param name="newsId">新闻编号(>0根据编号查询，=0查询所有，-1根据分类查询，其它查询)</param>
        /// <param name="tilte">标题</param>
        /// <param name="classifyid">分类</param>
        /// <returns>新闻列表</returns>
        public static List<ENews> GetByCondition(int newsId, string tilte, int classifyid)
        {
            List<ENews> newsList = new List<ENews>();
            object[] paramvalues = new object[] { newsId, tilte, classifyid };
            StoreProcedure sp = new StoreProcedure(_con, "news_selectbycondition");
            SqlDataReader reader = sp.ExecuteDataReader(paramvalues);

            while (reader.Read())
            {
                newsList.Add(new ENews()
                {
                    NewsId = Convert.ToInt32(reader["NewsId"]),
                    Title = Convert.ToString(reader["Title"]),
                    ImageName = Convert.ToString(reader["ImageName"]),
                    Contents = Convert.ToString(reader["Contents"]),
                    Author = Convert.ToString(reader["Author"]),
                    Source = Convert.ToString(reader["Source"]),
                    WriteTime = Convert.ToDateTime(reader["WriteTime"]),
                    Hits = Convert.ToInt32(reader["Hits"]),
                    Isvalid = Convert.ToBoolean(reader["Isvalid"]),
                    Keywords = Convert.ToString(reader["Keywords"]),
                    Description = Convert.ToString(reader["Description"]),
                    ClassifyId = Convert.ToInt32(reader["ClassifyId"]),
                    SortId = Convert.ToInt32(reader["SortId"]),
                    ClassifyName = Convert.ToString(reader["ClassifyName"]),
                });
            }

            reader.Close();
            return newsList;
        }
    }
}
